package tw.edu.tku.im.maze;

import org.json.JSONException;
import org.json.JSONObject;

import tw.edu.tku.im.maze.database.MarkProcessor;

public class GameState {
   
   boolean finished;
   boolean pass;
   Bonus bonus;
   
   //-------------------------------------------------------
   public GameState(boolean finished, boolean pass) {
      this(finished, pass, null);
   }
   
   public GameState(boolean finished, boolean pass, int exp) {
      this(finished, pass, new Bonus(exp));
   }
   
   public GameState(boolean finished, boolean pass, Bonus bonus) {
      this.finished = finished;
      this.pass = pass;
      this.bonus = bonus;
   }
   
   //-------------------------------------------------------
   public static GameState parse(JSONObject jsonObj) throws JSONException {
      boolean finished = jsonObj.getBoolean("finished");
      boolean pass = jsonObj.getBoolean("pass");
      if(jsonObj.isNull("bonus")) { 
         return new GameState(finished, pass);
      }
      else {
         Bonus bonus = Bonus.parse(jsonObj.getJSONObject("bonus"));
         return new GameState(finished, pass, bonus);
      }
   }
   
   //-------------------------------------------------------
   public boolean isFinished() {
      return finished;
   }

   public void setFinished(boolean finished) {
      this.finished = finished;
   }

   public boolean isPass() {
      return pass;
   }

   public void setPass(boolean pass) {
      this.pass = pass;
   }

   public Bonus getBonus() {
      return bonus;
   }

   public void setBonus(Bonus bonus) {
      this.bonus = bonus;
   }
   
   //-------------------------------------------------------
   public String toString() {
      StringBuilder ans = new StringBuilder("{ \"finished\" : ");
      ans.append(finished).append(", ").append("\"pass\" : ")
         .append(pass).append(", ").append("\"bonus\" : ")
         .append(this.bonus).append(" }");
      return ans.toString();
   }
   
   //-------------------------------------------------------
   public String toJSONString() throws JSONException {
      JSONObject gameState = new JSONObject();
      gameState.put("finished", this.finished);
      gameState.put("pass", this.pass);
      if(this.bonus!=null) {
         gameState.put("bonus", new JSONObject(this.bonus.toJSONString()));
      }
      return gameState.toString();
   }

}
